//
//  WSAlbumsDetailViewController.m
//  GroupFourFM
//
//  Created by 王鑫 on 15/12/23.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSAlbumsDetailViewController.h"
#import "WSAlbumDetailTableViewCell.h"
#import "WSAlbumHeaderView.h"
#import <UIButton+WebCache.h>
#import "WSAlbumsSectionHeadView.h"
#import "WSPlayViewController.h"
#import "WHAuthorDetailViewController.h"


@interface WSAlbumsDetailViewController ()<UITableViewDataSource, UITableViewDelegate, WSAlbumsSectionHeadViewDelegate, WSPlayViewControllerDelegate>

@property(nonatomic,strong)UITableView *albumTableView;
@property(nonatomic,strong)NSMutableArray *allTrackArray;
@property(nonatomic,strong)WSAlbumModel *myAlbum;
@property(nonatomic,strong)WSAlbumHeaderView *tableHeadView;
@property(nonatomic,assign)NSInteger currentPage;
@property(nonatomic,strong)UIImageView *overImage;
@property(nonatomic,strong)FXBlurView *blurView;
@property(nonatomic,strong)MBProgressHUD *hud;


@end

@implementation WSAlbumsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"专辑详情";
    self.view.backgroundColor = WHITE;
    
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = CLEAR;
    backButton.tintColor = WHITE;
    [backButton setImage:[[UIImage imageNamed:@"btn_back_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"album_album_mask"] forBarMetrics:UIBarMetricsCompactPrompt];
    
    //初始化数组
    self.allTrackArray = [NSMutableArray array];
    [self createSubviews];
    [self getData];
    self.currentPage = 1;
    
    /**
     *  小菊花
     */
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.hud.labelText = @"更多 • 加载";
}

- (void)createSubviews
{
    
    self.albumTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    self.albumTableView.backgroundColor = CLEAR;
    [self.view addSubview:self.albumTableView];
    self.albumTableView.dataSource = self;
    self.albumTableView.delegate = self;
    
    [self.albumTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(@0);
    }];
    
    // 注册cell
    [self.albumTableView registerClass:[WSAlbumDetailTableViewCell class] forCellReuseIdentifier:@"WSAlbumDetailTableViewCell"];
    
    [self.albumTableView reloadData];
    UIView *footerView = [[UIView alloc] init];
    self.albumTableView.tableFooterView = footerView;
    
    self.tableHeadView = [[WSAlbumHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 0.3)];
    self.tableHeadView.backgroundColor = CLEAR;
    self.albumTableView.tableHeaderView = self.tableHeadView;
    
    self.albumTableView.rowHeight = 120.f;
    self.albumTableView.sectionHeaderHeight = SCREEN_HEIGHT*0.06;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.albumTableView.mj_footer = footer;
}

#pragma mark - tableView的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allTrackArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WSAlbumDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WSAlbumDetailTableViewCell"];
    WSTrackModel *model = [self.allTrackArray objectAtIndex:indexPath.row];
    
    cell.trackModel = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
    player.delegate = self;
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
    WSTrackModel *sing = [self.allTrackArray objectAtIndex:indexPath.row];
    player.song_id = sing.trackId;
    player.allMusicArray = self.allTrackArray;
    player.indexRow = indexPath.row;
    player.tabbar = (WSTabBarController *)self.tabBarController;
    [self presentViewController:naVC animated:YES completion:nil];
}

#pragma mark - section头视图

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    WSAlbumsSectionHeadView *headView = [[WSAlbumsSectionHeadView alloc] init];
    headView.num = self.myAlbum.tracks;
    headView.delegate = self;
    return headView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.y > SCREEN_HEIGHT * 0.3 - 64) {
        
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsCompactPrompt];
    }
    else {
    
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"album_album_mask"] forBarMetrics:UIBarMetricsCompactPrompt];
    }
}

#pragma mark - 排序方法
-(void)sortListWithButton:(UIButton *)button {
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:_allTrackArray];
    self.allTrackArray = [NSMutableArray array];
    for (NSInteger i = array.count-1; i>=0; i--) {
        
        [self.allTrackArray addObject:[array objectAtIndex:i]];
    }
    [self.albumTableView reloadData];
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 请求数据
-(void)getData {
    
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/album/track/%@/true/%ld/20?position=2&albumId=%@&isAsc=true&device=iphone&title=%@&pageSize=20",self.albumId,self.currentPage,self.albumId , self.myTitle];
    
    [WSAppTools getDataWithURL:urlStr block:^(id result) {
        

        self.myAlbum = [WSAlbumModel modelWithDictionary:[result objectForKey:@"album"]];
        self.tableHeadView.albumModel = self.myAlbum;
        NSMutableDictionary *trackDic = [result objectForKey:@"tracks"];
        
        for (NSMutableDictionary *dic in [trackDic objectForKey:@"list"]) {
            
            WSTrackModel *model = [WSTrackModel modelWithDictionary:dic];
            [self.allTrackArray addObject:model];
        }
        
        
        [self.hud hide:YES];
        [self.albumTableView reloadData];
        self.currentPage ++;
        
        [self.albumTableView.mj_footer endRefreshing];
        
    } error:^(NSError *error) {
        
        NSLog(@"error = %@", error);
    }];
}

#pragma mark - 排序按钮方法实现
- (void)sortAction:(UIButton *)btn
{
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.allTrackArray];
    self.allTrackArray = [NSMutableArray array];
    for (NSInteger i = array.count-1; i>=0; i--) {
        [self.allTrackArray addObject:[array objectAtIndex:i]];
    }
    
    [self.albumTableView reloadData];
}

#pragma mark WSPlayViewControllerDelegate
- (void)changePageData:(NSInteger)id title:(NSString *)title {

    WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
    
    [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
    albumsDetailVC.albumId = [NSString stringWithFormat:@"%ld", id];
    albumsDetailVC.myTitle = title;
    [self.navigationController pushViewController:albumsDetailVC animated:YES];
}

- (void)pushNextViewC:(NSInteger)uid title:(NSString *)title {

    WHAuthorDetailViewController *authorDVC = [[WHAuthorDetailViewController alloc] init];
    authorDVC.uid = uid;
    authorDVC.title = title;
    [self.navigationController pushViewController:authorDVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
